<template>
  <div class="container">
    <el-card shadow="hover" class="card">
      <select-estate ref="estate" @setAd="setAd"></select-estate>
      <select-new ref="news" @setAd="setAd"></select-new>
      <el-form
        ref="dynamicValidateForm"
        :model="dynamicValidateForm"
        label-width="100px"
        class="demo-dynamic"
      >
        <el-col :span="15" :offset="4">
          <el-form-item
            v-for="(domain, index) in dynamicValidateForm.domains"
            :key="index"
            :label="'广告位' + index"
          >
            <!-- :rules="{
              required: true,
              message: '广告不能为空！',
              trigger: 'blur',
            }" -->
            <el-row :gutter="10">
              <el-col :span="18">
                <el-input
                  v-model="domain.id"
                  placeholder="请设置广告"
                  clearable
                  prefix-icon="el-icon-link"
                  :style="{ width: '100%' }"
                  disabled
                >
                  <div slot="append">
                    <el-button
                      size="small"
                      type="primary"
                      icon="el-icon-upload"
                      @click.stop="setEstateId(domain)"
                    >
                      设置芯房
                    </el-button>
                    <el-button
                      size="small"
                      type="primary"
                      icon="el-icon-upload"
                      @click.stop="setNew(domain)"
                    >
                      设置资讯
                    </el-button>
                  </div>
                </el-input>
              </el-col>
              <!-- <el-col :span="4">
                <el-button @click.prevent="removeDomain(domain)">
                  删除
                </el-button>
              </el-col> -->
            </el-row>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="8">
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm('dynamicValidateForm')"
            >
              提交/修改
            </el-button>

            <!-- <el-button @click="addAD">新增广告</el-button> -->
            <!-- <el-button @click="resetForm('dynamicValidateForm')">
              重置
            </el-button> -->
          </el-form-item>
        </el-col>
      </el-form>
    </el-card>
  </div>
</template>
<script>
  import { addConstant, getByType, modConstant } from '@/api/tag'
  import { uuid } from '@/utils/index'
  import _ from 'lodash'
  import SelectEstate from './components/SelectEstate.vue'
  import SelectNew from './components/SelectNew.vue'

  export default {
    name: 'Ad',
    components: { SelectEstate, SelectNew },
    data() {
      return {
        dynamicValidateForm: {
          domains: [],
        },
      }
    },
    created() {
      this.fetchAdData()
    },
    mounted() {},
    methods: {
      async fetchAdData() {
        let pushData = {}
        pushData.type = 3
        const { data } = await getByType(pushData)
        // this.houseTags = data.body
        let array = data.body
        let temp = []
        for (let index = 0; index < 5; index++) {
          const element = JSON.parse(array[index].constantValue)
          element.constantId = array[index].constantId
          temp.push(element)
        }
        this.dynamicValidateForm.domains = temp
      },
      setAd(childData) {
        let temp = childData
        for (let i = 0; i < this.dynamicValidateForm.domains.length; i++) {
          const element = this.dynamicValidateForm.domains[i]
          if (temp.tempData.key === element.key) {
            this.dynamicValidateForm.domains[i].id = childData.id
            this.dynamicValidateForm.domains[i].type = childData.type
            this.dynamicValidateForm.domains[i].image = childData.image
          }
        }
        console.log(childData)
      },
      setEstateId(_data) {
        this.$refs['estate'].showEdit(_data)
        console.log('设置地产')
      },
      setNew(_data) {
        this.$refs['news'].showEdit(_data)
        console.log('设置资讯')
      },
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          console.log(valid)
          console.log(this.dynamicValidateForm)
          if (valid) {
            let list = _.cloneDeep(this.dynamicValidateForm.domains)
            let _flag = true
            for (let index = 0; index < list.length; index++) {
              let item = {}
              item.constantId = list[index].constantId
              item.constantType = 3
              item.constantValue = JSON.stringify(list[index])
              const { data } = await modConstant(item)
              if (data.status == 0) {
              } else {
                _flag = false
                break
              }
            }
            if (_flag) {
              this.$baseNotify('广告修改成功！', '成功', 'success', 'top-right')
            } else {
              this.$baseNotify('广告修改失败！', '失败', 'error', 'top-right')
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
      removeDomain(item) {
        var index = this.dynamicValidateForm.domains.indexOf(item)
        if (index !== -1) {
          this.dynamicValidateForm.domains.splice(index, 1)
        }
      },
      addAD() {
        this.dynamicValidateForm.domains.push({
          key: Date.now(),
          id: '',
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  .container {
    display: flex;
    flex-direction: column;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  .card {
    border-radius: 20px;
    transition: all 0.3s cubic-bezier(0, 0, 0.5, 1);
  }
</style>
